﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Word_Mojo
{
    /// <summary>
    /// 
    /// </summary>
    class BoardModel
    {
        #region Private Data Members

        private Tile[,] theGameBoard;
        private BoardAnalyzer theBoardAnalyzer;
        private PlayersGameTilesDecider theTilesDecider;

        private bool firstPlayHasBeenMade = false;

        #endregion

        #region Construction

        /// <summary>
        /// 
        /// </summary>
        public BoardModel()
        {
            this.theGameBoard = new Tile[9, 9];
            this.theBoardAnalyzer = new BoardAnalyzer();
            this.theTilesDecider = new PlayersGameTilesDecider();
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="theGameBoard"></param>
        /// <returns></returns>
        public int SubmitPlayToGetPointValue(Tile[,] theGameBoard)
        {
            this.firstPlayHasBeenMade = true;
            int points = this.theBoardAnalyzer.submitPlayToGetPointValue(theGameBoard);
            this.theGameBoard = this.theBoardAnalyzer.getGameBoard();
            return points;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Tile[,] GetCurrentGameBoard()
        {
            return this.theGameBoard;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<string> GetSevenLettersForPlayersTiles()
        {
            //still holds true from previous game
            //if (!this.firstPlayHasBeenMade)
            //{
                //return this.theTilesDecider.getSevenLettersForPlayersTiles();
            //}

            //List<Tuple<int, Tile>> startsWithTiles = this.theBoardAnalyzer.getPlayableTilesForWordStart();

            //List<Tuple<int, Tile>> endsWithTiles = this.theBoardAnalyzer.getPlayableTilesForWordEnd();

            //return this.theTilesDecider.getSevenLettersForPlayersTiles(startsWithTiles, endsWithTiles);
            return this.theTilesDecider.getSevenLettersForPlayersTiles();
        }

        /// <summary>
        /// Returns a list of string suggestions
        /// </summary>
        /// <returns></returns>
        public List<string> GetWordSuggestions()
        {
            return null;
        }

        /// <summary>
        /// Retrieves a collection of strings representing each valid word on the board.  Used by the Form 
        /// in the game-over-information to display all the words the player made on the board.  
        /// </summary>
        /// <returns>A list of strings representing all the words on the board</returns>
        public List<string> GetWordsOnBoard(Tile[,] gameBoard)
        {
            List<GamePlay> gamePlays = theBoardAnalyzer.GetGamePlaysOnBoard(gameBoard);
            List<string> words = new List<string>();

            foreach (GamePlay play in gamePlays)
            {
                StringBuilder sBuilder = new StringBuilder();

                foreach (Tuple<int, int, Tile> tile in play.getTiles())
                {
                    sBuilder.Append(tile.Item3.Letter);
                }

                words.Add(sBuilder.ToString());
            }

            return words;
        }

        #endregion
    }
}
